package main

import (
	"bufio"
	"errors"
	"fmt"
	"golang.org/x/text/transform"
	"io"
	"io/ioutil"
	"net/http"

	"golang.org/x/net/html/charset"
	"golang.org/x/text/encoding"
)

//检测网页编码
func determineEncoding(r io.Reader) encoding.Encoding {
	bytes, err := bufio.NewReader(r).Peek(1024)
	if err != nil {
		panic(err)
	}
	e, _, _ := charset.DetermineEncoding(bytes, "")
	return e
}
func TryRecover() {
	defer func() {
		r := recover()
		fmt.Println(r)
	}()
	panic(errors.New("我是测试ERROR"))
}
func main() {
	//defer func() {
	//	r := recover()
	//	fmt.Println(r)
	//}()
	//TryRecover()
	//var user tree.Person
	//user.Age = 18
	//user.Name = "刘备"
	//user.Sex = "男"
	//fmt.Println(user)
	//queue := pop.Queue{1, 2, 3, 4, 4, 5}
	//h := queue.Pop()
	//fmt.Println(queue)
	//fmt.Println(h)
	//fmt.Printf("%p", &queue)
	//fmt.Printf("%p", &h)
	resp, err := http.Get("https://b2b.baidu.com/s?mk=%E5%8C%96%E5%B7%A5%E8%83%BD%E6%BA%90%E5%B8%82%E5%9C%BA&from=index_menu&fid=0%2C1634609043122&tf=&pi=b2b.index.menu")
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	e := determineEncoding(resp.Body)
	//fmt.Println(e)
	utf8Reader := transform.NewReader(resp.Body, e.NewDecoder())
	all, err := ioutil.ReadAll(utf8Reader)
	fmt.Println(string(all))
}
